package Ep06_BinaryTree.BT12_0110;

import Ep06_BinaryTree.TreeNode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/5/4 19:10
 * 说明：层序是个好思路，但是会超时
 */

public class Mine {
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        if(root.left == null && root.right == null) return true;
        int countLeft = 0;
        int countRight = 0;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while(!deque.isEmpty()){
            int size = deque.size();
            while(size > 0){
                TreeNode tempNode = deque.poll();

                if (root.left != null){
                    deque.offer(root.left);
                    countLeft++;
                }
                if (root.right != null){
                    deque.offer(root.right);
                    countRight++;
                }
                size--;
            }
        }
        return (countLeft-countRight >= 2 || countLeft-countRight <= -2);
    }
}
